<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>ajax</title>
    </head>
    <body>

        <script>

            //遍历批量操作数组元素
            const nums = [10,20,30,40,50]

            let newNums = []
            for (const n of nums) {
                if(n < 100){
                    newNums.push(n)
                }
            }
            console.log(newNums);

            let newNewNums = []
            for (const n of newNums) {
                newNewNums.push(n*2)
            }
            console.log(newNewNums);

            let total = 0
            for (const n of newNewNums) {
                total += n
            }
            console.log(total);

            //批量操作函数式编程
            //filter
            let newNums1 = nums.filter(function(n){
                return n < 100
            })
            console.log(newNums1);
            //map
            let newNewNums1 = newNums1.map(function(n){
                return n * 2
            })
            console.log(newNewNums1);
            //reduce
            let total1 = newNewNums1.reduce(function(preValue,n){
                return preValue + n
            },0)
            console.log(total1);

            //链式
            let totalnums = nums.filter(function(n){
                return n < 100
            }).map(function(n){
                return n * 2
            }).reduce(function(preValue,n){
                return preValue + n
            },0)
            console.log(totalnums);

            //简化语法糖
            let totalnums2 = nums.filter(n => n < 100).map(n=> n * 2 ).reduce((preValue,n)=> preValue + n,0)
            console.log(totalnums);

        </script>
    </body>
</html>